package com.zhizuobiao.coura.stockhelper;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhizuobiao.coura.adapter.MyPagerAdapter;
import com.zhizuobiao.coura.fragment.HASFragment;
import com.zhizuobiao.coura.fragment.HHSFragment;
import com.zhizuobiao.coura.fragment.HKSFragment;
import com.zhizuobiao.coura.fragment.SHSFragment;
import com.zhizuobiao.coura.receiver.NetBroadcastReceiver;
import com.zhizuobiao.coura.utils.NetUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, NetBroadcastReceiver.NetEvent {

    @BindView(R.id.tv_stockType)
    TextView tvStockType;
    @BindView(R.id.tv_Chg)
    TextView tvChg;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.line2)
    View line2;
    @BindView(R.id.line3)
    View line3;
    @BindView(R.id.line4)
    View line4;
    @BindView(R.id.tv_HKS)
    TextView tvHKS;
    @BindView(R.id.tv_HAS)
    TextView tvHAS;
    @BindView(R.id.tv_SHS)
    TextView tvSHS;
    @BindView(R.id.tv_HHS)
    TextView tvHHS;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.tv_weather_switch)
    TextView tvWeatherSwitch;
    @BindView(R.id.tv_net)
    TextView tvNet;
    private MyPagerAdapter myPagerAdapter;
    private List<Fragment> list;
    private FragmentManager fragmentManager;
    private HKSFragment hksFragment;
    private HASFragment hasFragment;
    private SHSFragment shsFragment;
    private HHSFragment hhsFragment;
    private boolean flag1, flag2, flag3, flag4;
    public static NetBroadcastReceiver.NetEvent netEvent;
    private int netMobile;  //保存手机网络连接状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        list = new ArrayList<Fragment>();
        hksFragment = new HKSFragment();
        hasFragment = new HASFragment();
        shsFragment = new SHSFragment();
        hhsFragment = new HHSFragment();
        list.add(hksFragment);
        list.add(hasFragment);
        list.add(shsFragment);
        list.add(hhsFragment);
        fragmentManager = getSupportFragmentManager();
        myPagerAdapter = new MyPagerAdapter(fragmentManager, list);
        viewpager.setAdapter(myPagerAdapter);
        viewpager.addOnPageChangeListener(this);
        viewpager.setCurrentItem(0);
        netEvent = this;
        if (inspectNet()){
            tvNet.setVisibility(View.GONE);
        }else{
            tvNet.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 涨跌幅点击事件
     *
     * @param view
     */
    @OnClick(R.id.tv_Chg)
    public void sort(View view) {
        switch (viewpager.getCurrentItem()) {
            case 0:
                if (flag1) {
                    hksFragment.resetAdapter1();
                    flag1 = false;
                } else {
                    hksFragment.resetAdapter2();
                    flag1 = true;
                }
                break;
            case 1:
                if (flag2) {
                    hasFragment.resetAdapter1();
                    flag2 = false;
                } else {
                    hasFragment.resetAdapter2();
                    flag2 = true;
                }
                break;
            case 2:
                if (flag3) {
                    shsFragment.resetAdapter1();
                    flag3 = false;
                } else {
                    shsFragment.resetAdapter2();
                    flag3 = true;
                }
                break;
            case 3:
                if (flag4) {
                    hhsFragment.resetAdapter1();
                    flag4 = false;
                } else {
                    hhsFragment.resetAdapter2();
                    flag4 = true;
                }
                break;

        }

    }

    /**
     * 股票列表选择
     *
     * @param view
     */
    @OnClick({R.id.tv_HKS, R.id.tv_HAS, R.id.tv_SHS, R.id.tv_HHS})
    public void onClick(View view) {
        changeState(view.getId());
    }


    /**
     * 清除状态（比如字体颜色）
     */
    private void clearState() {
        tvHKS.setTextColor(getResources().getColor(R.color.colorGrey));
        line1.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        tvHAS.setTextColor(getResources().getColor(R.color.colorGrey));
        line2.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        tvSHS.setTextColor(getResources().getColor(R.color.colorGrey));
        line3.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        tvHHS.setTextColor(getResources().getColor(R.color.colorGrey));
        line4.setBackgroundColor(getResources().getColor(R.color.colorWhite));
    }

    /**
     * viewpager改变状态
     *
     * @param num
     */
    private void changeState(int num) {
        switch (num) {
            case 0:
            case R.id.tv_HKS:
                clearState();
                viewpager.setCurrentItem(0);
                tvStockType.setText("港股列表");
                tvHKS.setTextColor(getResources().getColor(R.color.colorBlue));
                line1.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                break;
            case 1:
            case R.id.tv_HAS:
                clearState();
                viewpager.setCurrentItem(1);
                tvStockType.setText("美股列表");
                tvHAS.setTextColor(getResources().getColor(R.color.colorBlue));
                line2.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                break;
            case 2:
            case R.id.tv_SHS:
                clearState();
                viewpager.setCurrentItem(2);
                tvStockType.setText("深股列表");
                tvSHS.setTextColor(getResources().getColor(R.color.colorBlue));
                line3.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                break;
            case 3:
            case R.id.tv_HHS:
                clearState();
                viewpager.setCurrentItem(3);
                tvStockType.setText("沪股列表");
                tvHHS.setTextColor(getResources().getColor(R.color.colorBlue));
                line4.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                break;
        }
    }

    //实现ViewPager.OnPageChangeListener接口后重写的方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        changeState(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @OnClick(R.id.tv_weather_switch)
    public void switchToWeather() {
        Intent intent = new Intent(MainActivity.this, WeatherActivity.class);
        startActivity(intent);
    }


    /**
     * 初始化时判断有没有网络
     */
    public boolean inspectNet() {
        this.netMobile = NetUtil.getNetWorkState(MainActivity.this);
        return isNetConnect();
        // if (netMobile == NetUtil.NETWORK_WIFI) {
        // System.out.println("inspectNet：连接wifi");
        // } else if (netMobile == NetUtil.NETWORK_MOBILE) {
        // System.out.println("inspectNet:连接移动数据");
        // } else if (netMobile == NetUtil.NETWORK_NONE) {
        // System.out.println("inspectNet:当前没有网络");
        //
        // }
    }

    /**
     * 判断有无网络 。
     *
     * @return true 有网, false 没有网络.
     */
    public boolean isNetConnect() {
        if (netMobile == NetUtil.NETWORK_WIFI) {
            return true;
        } else if (netMobile == NetUtil.NETWORK_MOBILE) {
            return true;
        } else if (netMobile == NetUtil.NETWORK_NONE) {
            return false;
        }
        return false;
    }

    @Override
    public void onNetChange(int netMobile) {
        this.netMobile = netMobile;
        isNetConnect();
        //网络状态变化时的操作
        if (netMobile == NetUtil.NETWORK_NONE) {
            tvNet.setVisibility(View.VISIBLE);
        } else {
            tvNet.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.tv_net)
    public void onClick() {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        startActivity(intent);
    }
}
